package acom.test.swing.aa;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class MainView implements Observer{
	
	static PrizeJFrame prizeJFrame ;
	private static List<Prize> prizelist =new ArrayList<Prize>();
	
	public JFrame frame;
	public JPanel contentPane;
	public JLabel titleName_lb;
	public JComboBox comboBox;
	
	
	public JButton openfile_but;
	public JButton setPrize_but;
	
	public JButton run_but;
	public JButton stop_but;
		
	public Font font;
	
	public static int window_width = 500;
	public static int window_height = 600;
	
	public static int position_vertical=100;
	public static int position_horizontal=45;
	
	public static int widget_width = 150;
	public static int widget_height = 30;
	
			
	//String[] fontsize = { "请选择奖项"};
	
	public MainView(){
		
		frame = new JFrame();
		contentPane = new JPanel();
		titleName_lb = new JLabel("选择的奖项：", JLabel.CENTER);
		comboBox = new JComboBox();
		comboBox.setBorder(BorderFactory.createTitledBorder("请选择奖项:"));
		
		openfile_but = new JButton("导入名单");
		setPrize_but = new JButton("设置奖项");
		
		run_but = new JButton("开  始");
		stop_but = new JButton("结  束");
	}
	
	public void init(){
		//设置 基本 显示属性
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭框架的同时结束程序
		frame.setSize(window_width, window_height);//设置框架大小为长300,宽200
		frame.setResizable(false);//设置框架不可以改变大小
		frame.setTitle("下拉框 选择事件");		
		contentPane.setLayout(null);//设置面板布局管理
		contentPane.setBackground(Color.lightGray);// 设置面板背景颜色
		
		font = new Font("SansSerif", Font.PLAIN, 15);
		titleName_lb.setFont(font);
		
		try {
			// 将LookAndFeel设置成Windows样式
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		//设置大小高度 属性
		comboBox.setBounds(position_horizontal, position_vertical, 150, 50);
		titleName_lb.setBounds(position_horizontal+200, position_vertical, 150, widget_height);
						
		openfile_but.setBounds(position_horizontal, position_vertical-70, 120, 30);
		setPrize_but.setBounds(position_horizontal+150, position_vertical-70, 120, 30);
		
		run_but.setBounds(position_horizontal, position_vertical+300, 120, 30);
		stop_but.setBounds(position_horizontal+150, position_vertical+300, 120, 30);
		
		//事件
		comboBox.addItemListener( new ItemListener(){
			@Override
			public void itemStateChanged(ItemEvent e) {
				itemStateChanged_Action(e);
			}			
		});
		
		openfile_but.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent e) {
				//打开文件选择器
				JOptionPane.showMessageDialog(null,"打开文件选择器");
			}
		});
		setPrize_but.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent e) {
				prizeJFrame.show();
			}
		});
		run_but.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"开始 抽奖");
			}
		});
		stop_but.addActionListener(new ActionListener(){			
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"结束");
			}
		});
		
		//添加组件
		contentPane.add(comboBox);
		contentPane.add(titleName_lb);
						
		contentPane.add(openfile_but);
		contentPane.add(setPrize_but);
		contentPane.add(run_but);
		contentPane.add(stop_but);
		
		frame.add(contentPane);
		frame.setVisible(true);
		showInCenter(frame);
	}		
	
	private void insertComboBox(String item){
		comboBox.addItem(item);// 插入项目tmp到0索引位置(第一列中).	
	}

	protected void itemStateChanged_Action(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) // 当用户的选择改变时，则在JLabel上会显示出Swing目前字形大小信息
		{
			String item=(String) e.getItem();
			titleName_lb.setText("点击 ‘开始’ 就可以抽：" + item);
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MainView mainView=new MainView();
		mainView.init();
		
		prizeJFrame = new PrizeJFrame();
		
		prizeJFrame.addObserver(mainView);
	}
	
	public void showInCenter(JFrame frame) {
		// 显示位置 在屏幕中间
		int HEIGHT = frame.getHeight();
		int WIDTH = frame.getWidth();
		Toolkit kit = Toolkit.getDefaultToolkit();// 设置顶层容器框架为居中
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		frame.setLocation(x, y);
	}

	//观察者模式
	@Override
	public void update(Observable o, Object arg) {
		//把增加的 奖项插入到 下拉框
		Prize prize=((PrizeJFrame)o).getPrize();
		String item =prize.getPname();
		insertComboBox(item);				
	}
	
	
}
